package com.myprocessing.aimation;

import com.myprocessing.action.Action;

import processing.core.PApplet;

public abstract class TimedBehavior {

	protected long stepInterval;			//Time in micros btw steps
	protected boolean active = false;
	protected int numOfSteps = 0;		//Total num of steps
	protected int currentStep = 0;		//Current Step in process
	protected PApplet pApplet;
	protected Action drawAction;
	protected Action stepAction;
	protected Action preAction;
	protected Action postAction;

	protected long lastStepMills;

	public TimedBehavior(){
	}
	
	public TimedBehavior(long stepInterval, int numOfSteps, PApplet applet,
			Action drawAction, Action stepAction) {
		this.stepInterval = stepInterval;
		this.numOfSteps = numOfSteps;
		pApplet = applet;
		this.drawAction = drawAction;
		this.stepAction = stepAction;
	}
	
	public TimedBehavior(long stepInterval, int numOfSteps, PApplet applet,
			Action drawAction, Action stepAction, Action preAction,
			Action postAction) {
		super();
		this.stepInterval = stepInterval;
		this.numOfSteps = numOfSteps;
		pApplet = applet;
		this.drawAction = drawAction;
		this.stepAction = stepAction;
		this.preAction = preAction;
		this.postAction = postAction;
	}

	public void runAnimation() {
		if (active) {
			drawAction.execute();
			
			long mills = System.currentTimeMillis();
			
			if(mills - lastStepMills > stepInterval){
				System.out.println(mills - lastStepMills);
				lastStepMills = mills;
				stepAction.execute();
				currentStep++;
				
				if(currentStep >= numOfSteps){
					currentStep = 0;
					deactivate();
				}
			}
		}
	}
	

	public void activate(){
		this.active = true;
		if(preAction != null)
			preAction.execute();
	}
	
	public void deactivate(){
		this.active = false;
		if(postAction != null)
			postAction.execute();
	}
	
	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public int getNumOfSteps() {
		return numOfSteps;
	}

	public void setNumOfSteps(int numOfSteps) {
		this.numOfSteps = numOfSteps;
	}

	public long getStepInterval() {
		return stepInterval;
	}

	public void setStepInterval(long stepInterval) {
		this.stepInterval = stepInterval;
	}

	public Action getPreAction() {
		return preAction;
	}

	public void setPreAction(Action preAction) {
		this.preAction = preAction;
	}

	public Action getPostAction() {
		return postAction;
	}

	public void setPostAction(Action postAction) {
		this.postAction = postAction;
	}

	public Action getDrawAction() {
		return drawAction;
	}

	public void setDrawAction(Action drawAction) {
		this.drawAction = drawAction;
	}

	public Action getStepAction() {
		return stepAction;
	}

	public void setStepAction(Action stepAction) {
		this.stepAction = stepAction;
	}
	
}
